/*!
 * classie - class helper functions
 * from bonzo https://github.com/ded/bonzo
 * 
 * classie.has( elem, 'my-class' ) -> true/false
 * classie.add( elem, 'my-new-class' )
 * classie.remove( elem, 'my-unwanted-class' )
 * classie.toggle( elem, 'my-class' )
 */

/*jshint browser: true, strict: true, undef: true */
/*global define: false */

(function(window) {

    'use strict';

    // class helper functions from bonzo https://github.com/ded/bonzo

    function classReg(className) {
        return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
    }

    // classList support for class management
    // altho to be fair, the api sucks because it won't accept multiple classes at once
    var hasClass, addClass, removeClass;

    if ('classList' in document.documentElement) {
        hasClass = function(elem, c) {
            return elem.classList.contains(c);
        };
        addClass = function(elem, c) {
            elem.classList.add(c);
        };
        removeClass = function(elem, c) {
            elem.classList.remove(c);
        };
    } else {
        hasClass = function(elem, c) {
            return classReg(c).test(elem.className);
        };
        addClass = function(elem, c) {
            if (!hasClass(elem, c)) {
                elem.className = elem.className + ' ' + c;
            }
        };
        removeClass = function(elem, c) {
            elem.className = elem.className.replace(classReg(c), ' ');
        };
    }

    function toggleClass(elem, c) {
        var fn = hasClass(elem, c) ? removeClass : addClass;
        fn(elem, c);
    }

    var classie = {
        // full names
        hasClass: hasClass,
        addClass: addClass,
        removeClass: removeClass,
        toggleClass: toggleClass,
        // short names
        has: hasClass,
        add: addClass,
        remove: removeClass,
        toggle: toggleClass
    };

    // transport
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(classie);
    } else {
        // browser global
        window.classie = classie;
    }

})(window);


(function() {
    var triggerBttn = document.getElementById('trigger-overlay'),
        overlay = document.querySelector('div.overlay'),
        closeBttn = overlay.querySelector('.g_hea_close');
    transEndEventNames = {
            'WebkitTransition': 'webkitTransitionEnd',
            'MozTransition': 'transitionend',
            'OTransition': 'oTransitionEnd',
            'msTransition': 'MSTransitionEnd',
            'transition': 'transitionend'
        },
        transEndEventName = transEndEventNames[Modernizr.prefixed('transition')],
        support = { transitions: Modernizr.csstransitions };
    s = Snap(overlay.querySelector('svg')),
        path = s.select('path'),
        steps = overlay.getAttribute('data-steps').split(';'),
        stepsTotal = steps.length;

    var elehide;

    function hide() {
        clearTimeout(elehide)
        elehide = setTimeout(function() {
            hides();
        }, 50)
    }

    function hides() {
        if (classie.has(overlay, 'open')) {
            $('.overlay').removeClass('overlay_re')
            var pos = stepsTotal - 1;
            classie.remove(overlay, 'open');
            classie.add(overlay, 'close');
            var onEndTransitionFn = function(ev) {
                    classie.remove(overlay, 'close');
                },
                nextStep = function(pos) {
                    pos--;
                    if (pos < 0) return;
                    path.animate({ 'path': steps[pos] }, 60, mina.linear, function() {
                        if (pos === 0) {
                            onEndTransitionFn();
                        }
                        nextStep(pos);
                    });
                };

            nextStep(pos);
        }
    }

    function show() {

        clearTimeout(elehide)
        if (!classie.has(overlay, 'open')) {
            var pos = 0;
            classie.add(overlay, 'open');

            var nextStep = function(pos) {
                pos++;
                if (pos > stepsTotal - 1) return;
                path.animate({ 'path': steps[pos] }, 60, mina.linear, function() { nextStep(pos); });
            };

            nextStep(pos);
            $('.g_hea_ul').find('li').removeClass('current').eq(0).addClass('current');
            $('.g_hea_tab').hide().eq(0).show().find(".g_hea_ul2 li").removeClass('current').eq(0).addClass('current');
            $('.g_hea_tab').eq(0).find('.g_hea_tab2').removeClass('current').eq(0).addClass('current');
        }
    }

    triggerBttn.addEventListener('click', function() {
        if (g_head_bool == 1) {
            return;
        }
        show();
    });
    //triggerBttn.addEventListener( 'mouseleave', hide );
    //closeBttn.addEventListener( 'mouseenter', show );
    closeBttn.addEventListener('click', function() {
        if (g_head_bool == 1) {
            g_head_bool = 0;
            localStorage.setItem('g_head_bool', g_head_bool);
            $('.switch_large input').removeAttr('checked');

        }
        hide();
    });
    window.g_head_bool = 0;
    window.g_headshow = show;
    window.g_headhide = hide;
})();

$(function() {
    $('.g_hea_ul').on('click', 'li', function() {
        $(this).parent().find('li').removeClass('current');
        $(this).addClass('current')
        $('.g_hea_tab').hide().eq($(this).index()).show();
        $('.g_hea_tab').eq($(this).index()).find('.g_hea_ul2').find('li').removeClass('current').eq(0).addClass('current');
        $('.g_hea_tab').eq($(this).index()).find(".g_hea_tab2").removeClass('current').eq(0).addClass('current');
    })
    $('.g_hea_ul2').on('click', 'li', function() {
        $(this).parent().find('li').removeClass('current');
        $(this).addClass('current');
        $(this).parents('.g_hea_tab').find('.g_hea_tab2').removeClass('current').eq($(this).index()).addClass('current');
    })
    $('.switch_large').on('click', 'input', function() {
        g_head_bool = $(this).get(0).checked ? 1 : 0;
        localStorage.setItem('g_head_bool', g_head_bool)
        if (g_head_bool == 1) {
            //$('.overlay').addClass('open')
            $('.overlay').addClass('overlay_re')
            g_headshow();

        } else {
            g_headhide();

        }
    })
    var t = localStorage.getItem('g_head_bool') || 0;
    console.log(t);
    g_head_bool = t;
    if (t == 1) {
        //setTimeout(function(){
        $('.switch_large input').attr('checked', true);
        $('.overlay').addClass('overlay_re')
        g_headshow();
        //},200)
    }
    $('.g_hea_login').on('click', 'span', function() {
        $(this).parent().find('div').toggle();
        $(this).toggleClass('cur')
    })
    $('body').on('click', function(e) {
            //console.log(e.target)
            var t = $(e.target);
            if (t.get(0).tagName != "span" && !t.hasClass('cur')) {
                $('.g_hea_login').find('span.cur').removeClass('cur');
                $('.g_hea_login').find('.userboxinfo').hide();
            }
        })
        // $('.g_hea_login').on('mouseover','span',function(){
        //   $(this).parent().find('div').show();
        //   $(this).addClass('cur')
        // }).on('mouseout','span',function(){
        //   $(this).parent().find('div').hide();
        //   $(this).removeClass('cur')
        // }).on('mouseover','div',function(){
        //   $(this).show();
        //   $(this).parent().find('span').addClass('cur')
        // }).on('mouseout','div',function(){
        //   $(this).hide();
        //   $(this).parent().find('span').removeClass('cur')
        //})
    $('body').on('click', '.g_hea_a1', function() {
        if (g_head_bool == 1) {
            $('.overlay').addClass('z_index_0');
        }

        // $('.g_hea_login').trigger('click')
    })
})